home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / offlnsft.lha / OfflineSafety / DoHighReadMagic.c < prev    next >
C/C++ Source or Header  |  1995-07-26  |  4KB  |  103 lines

  1. /* DoHighReadMagic.c */
  2.  
  3. #include "exec/io.h"
  4. #include "exec/memory.h"
  5. #include "exec/libraries.h"
  6. #include "exec/execbase.h"
  7. #include "dos/dos.h"
  8. #include "sh/sub.h"
  9. #include "techprg.h"
  10. #include "proto/exec_protos.h"
  11. #include "proto/dos_protos.h"
  12.  
  13. #define TRUE 1
  14. #define FALSE 0
  15.  
  16. int _main(void);
  17. void error(u_char *),cleanup(void);
  18.  
  19. struct UserVal *user;
  20. struct RdArds *ra;
  21.  
  22. BPTR file,bkup;
  23. ULONG args[5],len,*hr,num;
  24.  
  25. u_char template[]="PATH/A,BKUP,LOAD/S,SAVE/S,VIEW/S",bbuf[128],
  26.     ver[]="$VER: DoHighReadMagic 1.00",cmd[32],buf[128],userfile[4096];
  27.  
  28. #define arg_path    (u_char *)args[0]
  29. #define arg_bkup    (u_char *)args[1]
  30. #define arg_load    args[2]
  31. #define arg_save    args[3]
  32. #define arg_view    args[4]
  33.  
  34. int _main(void) {
  35. LONG i;
  36. u_char *ch,*bufch;
  37.     GetProgramName(cmd,32);
  38.     if(!(ra=ReadArgs(template,args,NULL))) {
  39.         PrintFault(i=IoErr(),cmd);
  40.         exit(i);
  41.     }
  42.     strcpy(buf,arg_path);
  43.     AddPart(buf,"User.dat",128);
  44.     strcpy(bbuf,arg_path);
  45.     AddPart(bbuf,arg_bkup?arg_bkup:(u_char *)"HighRead.backup",128);
  46.     if(file=Open(buf,MODE_OLDFILE)) {
  47.         Seek(file,0,OFFSET_END);
  48.         len=Seek(file,0,OFFSET_BEGINNING);
  49.         if(Read(file,userfile,len)==len) {
  50.             ch=userfile;
  51.             bufch=buf;
  52.             while(*ch && *ch!=10 && *ch!=13) *bufch++=*ch++;
  53.             *bufch=0;
  54.             for(i=0;i<6;ch++,i++)
  55.                 while(*ch && *ch!=10 && *ch!=13) ch++;
  56.             user=(struct UserVal *)ch;
  57.             hr=(ULONG *)(user+1);
  58.             num=*hr++;
  59.             if(arg_view) {
  60.                 print("Viewing message pointers for %ld areas.\n",num);
  61.                 for(i=0;i<num;i++) print("Highread for area %ld: %ld\n",
  62.                     i+1,hr[i]);
  63.             } else if(arg_load) {
  64.                 if(bkup=Open(bbuf,MODE_OLDFILE)) {
  65.                     Read(bkup,buf,8);
  66.                     if(*((ULONG *)buf)==(ULONG)'HRBK') {
  67.                         i=*((ULONG *)(buf+4));
  68.                         if(i<num) rawprint("There are less message pointers stored in the backup than there\nare in the user file. The rest of the pointers will remain untouched.\n");
  69.                             else if(i>num) rawprint("There are more message pointers stored in the backup than there\nare in the user file. The rest of the pointers will not be loaded.\n");
  70.                         print("Loading message pointers for %ld areas.\n",num>i?i:num);
  71.                         Read(bkup,hr,num*sizeof(ULONG));
  72.                         Seek(file,0,OFFSET_BEGINNING);
  73.                         Write(file,userfile,len);
  74.                     } else print("%s: not a valid backup file\n",cmd);
  75.                 } else print("%s: unable to open backup file\n",cmd);
  76.             } else if(arg_save) {
  77.                 if(bkup=Open(bbuf,MODE_NEWFILE)) {
  78.                     Write(bkup,"HRBK",4);
  79.                     Write(bkup,&num,4);
  80.                     print("Saving message pointers for %ld areas.\n",num);
  81.                     Write(bkup,hr,num*sizeof(ULONG));
  82.                 } else print("%s: unable to open backup file\n",cmd);
  83.             } else print("No operation.\n");
  84.             rawprint("©1995 by Sami Klemola.\n");
  85.         } else print("%s: read error\n",cmd);
  86.     } else print("%s: unable to open user file\n",cmd);
  87.     cleanup();
  88.     return(RETURN_OK);
  89. };
  90.  
  91. void error(u_char *str) {
  92.     print("%s: %s\n",cmd,str);
  93.     cleanup();
  94.     exit(RETURN_FAIL);
  95. };
  96.  
  97. void cleanup(void) {
  98.     if(bkup) Close(bkup);
  99.     if(file) Close(file);
  100.     if(ra) FreeArgs(ra);
  101. };
  102.  
  103.